﻿
//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////

#include "PickUtil.h"
#include "IType.h"
#include "IFamily.h"
#include "IElement.h"
#include "ICategory.h"
#include "IDocument.h"
#include "ElementId.h"
#include "UniIdentity.h"
#include "IInstanceType.h"
#include "ICategoryLibrary.h"
#include "IElementBasicInformation.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;
using namespace Sample;

std::wstring PickUtil::GetElementDescription(IDocument* pDoc, const ElementId& elementId )
{
    std::wstring res;
    DBG_WARN_AND_RETURN_UNLESS(pDoc, res, L"无效pDoc",L"GDMP",L"2024-03-30");
    DBG_WARN_AND_RETURN_UNLESS(elementId.IsValid(), res, L"无效elementId",L"GDMP",L"2024-03-30");

    const IElement* pElement = pDoc->GetElement(elementId);
    DBG_WARN_AND_RETURN_UNLESS(pElement, res, L"无效指针pElement",L"GDMP",L"2024-03-30");

    const IElementBasicInformation* pBasicInfo = pElement->GetBasicInformation();
    const UniIdentity categoryId = pBasicInfo->GetCategoryUid();
    const ICategoryLibrary* pCategoryLib = ICategoryLibrary::Get(pDoc);
    const ICategory* pCategory = pCategoryLib->GetCategory(categoryId);
    DBG_WARN_AND_RETURN_UNLESS(pCategory, res, L"无效指针pCategory",L"GDMP",L"2024-03-30");

    res += pCategory->GetDisplayName();
    if (const IType* pType = pElement->GetBasicInformation()->GetType())
    {
        const IInstanceType* pInstanceType = quick_cast<const IInstanceType>(pType);
        if (pInstanceType)
        {
            if (const IFamily* pFamily = pInstanceType->GetFamily())
                res = res + L" : " + pFamily->GetBasicInformation()->GetName();
        }
        
        res = res + L" : " + pType->GetBasicInformation()->GetName();
    }

    return res;
}

